package dataStream;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 读取和写入一个二进制图片文件
 * 
 * @author yy
 *
 */

public class DataStreamDemo {

	public static void main(String[] args) {
		FileInputStream fis = null;
		DataInputStream dis = null;
		FileOutputStream fos = null;
		DataOutputStream dos = null;

		try {
			// 构建二进制数据的输入流
			fis = new FileInputStream("e:/t01/流浪狗.jpg");
			dis = new DataInputStream(fis);
			// 构建二进制数据的输出流
			fos = new FileOutputStream("e:/t01/流浪狗小巴克.jpg");
			dos = new DataOutputStream(fos);
			// 读取和写入文件
			int len;
			while ((len = dis.read()) != -1) {
				dos.write(len);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				// 关闭流
				dos.close();
				fos.close();
				dis.close();
				fis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
